package com.luan.xiaopozhan.controller;


import com.luan.xiaopozhan.entity.NewsTable;
import com.luan.xiaopozhan.entity.RestResult;
import com.luan.xiaopozhan.repository.NewsRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "新闻相关")
@RestController
@RequestMapping("/api")
public class NewsController {

    @Autowired
    NewsRepository newsRepository;

    @ApiResponses(value = {
            @ApiResponse(responseCode ="400",description ="请求参数没填好"),
            @ApiResponse(responseCode  = "200", description ="测试",content = {
                    @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = RestResult.class)))
            }),
    })
    @ApiOperation("获取新闻列表")
    @GetMapping("/getNewsList")
    @ResponseBody
    public RestResult<List<NewsTable>> getNewsList() {
        List<NewsTable> newsTables = newsRepository.findAll();
        if (!newsTables.isEmpty()) {
            return RestResult.success().data(newsTables);
        } else {
            return RestResult.success().data(null);
        }
    }
}
